from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

from selenium.webdriver.support.select import Select

sys.path.append(r'..')
from web_base.base_page import BasePage
from page_object.login_page import LoginPage


class AssetPage(BasePage):
    url = r'/bsams/front/asset/asset_list.do'
    # 新增定位
    add_btn_loc = (By.CLASS_NAME, 'button_true')
    add_title_loc = (By.ID, 'title')
    add_providerId_loc = (By.ID, 'providerId')
    add_storageId_loc = (By.ID, 'storageId')
    add_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')

    # 修改定位
    mod_btn_loc = (By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[1]')
    mod_title_loc = (By.ID, 'title')
    mod_providerId_loc = (By.ID, 'providerId')
    mod_storageId_loc = (By.ID, 'storageId')
    mod_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')

    # 查询定位
    query_status_loc = (By.ID, 'status')
    query_title_loc = (By.ID, 'title')
    query_btn_loc = (By.CLASS_NAME, 'search_button')

    # 借用定位
    lend_btn_loc = (By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[3]/td[8]/a[2]')
    lend_dep_loc = (By.ID, 'departId')
    lend_remark_loc = (By.ID, 'remark')
    lend_ok_loc = (By.XPATH, '//*[@id="fm_new"]/div/div[1]')

    # 归还定位
    return_btn_loc = (By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[2]')
    return_ok_loc = (By.XPATH, '//*[@id="cboxLoadedContent"]/div/div/div[2]/div[2]/div[1]')

    # 新增
    def asset_add(self, title, providerId, storageId):
        self.open()
        self.ele_click(self.add_btn_loc)
        self.input_ele(self.add_title_loc, title)
        self.ele_select(self.add_providerId_loc, providerId)
        self.ele_select(self.add_storageId_loc, storageId)
        self.ele_click(self.add_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    # 修改
    def asset_mod(self, title, providerId, storageId):
        self.open()
        self.ele_click(self.mod_btn_loc)
        self.input_ele(self.mod_title_loc, title)
        self.ele_select(self.mod_providerId_loc, providerId)
        self.ele_select(self.mod_storageId_loc, storageId)
        self.ele_click(self.mod_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    # 查询
    def asset_query(self, status, title):
        self.open()
        self.ele_select(self.query_status_loc, status)
        self.input_ele(self.query_title_loc, title)
        self.ele_click(self.query_btn_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    # 借用
    def asset_lend(self, departId, remark):
        self.open()
        self.ele_click(self.lend_btn_loc)
        self.ele_select(self.lend_dep_loc, departId)
        self.input_ele(self.lend_remark_loc, remark)
        self.ele_click(self.lend_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    # 归还
    def asset_return(self):
        self.open()
        self.ele_click(self.return_btn_loc)
        self.ele_click(self.lend_ok_loc)
        text = self.get_alert_text()
        self.alert_click()
        return text


if __name__ == '__main__':
    title = '联想小新01'
    providerId = '3'
    storageId = '1'

    chrome_location = r'D:\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)

    login_page = LoginPage(driver)
    login_page.login()

    asset_page = AssetPage(driver)
    text = asset_page.asset_add(title, providerId, storageId)
    print(text)

    driver.quit()
